<?php
/**
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 *
 * Author: YSX
 * Date: 2023/04/05
 */
namespace app\admin\controller;

use app\common\controller\Backend;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;
class Page extends Backend{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function index(){
        if (Request::isPost()) {
            $keys = $this->request->post('keys', '', 'trim');
            $page = $this->request->post('page') ? $this->request->post('page') : 1;
            $limit = $this->request->post('limit') ? $this->request->post('limit') : 15;

            $condition = [];
            if (!empty($keys)) {
                $condition[] = ['name', 'like', '%' . $keys . '%'];
            }
            $list = Db::name('page')
                ->order('id desc')
                ->paginate(['list_rows' => $limit, 'page' => $page])
                ->toArray();

            return $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['data'], 'count' => $list['total']];
        }
        return view();

    }

    public function add(){
        if (Request::isPost()) {
            $params = $this->request->post();
   			$data = [
   				'name'=>$params['name'],
   				'bus_code'=>$params['bus_code'],
   				'url_address'=>$params['url_address'],
   				'create_time'=>time(),
   			];
            $res = Db::name('page')->insert($data);
            if ($res) {
                $this->success(lang('add success'),url('index'));
            } else {
                $this->error(lang('add fail'));
            }
        }
        $view = [
            'info' => '',
            'title' => lang('add'),
        ];
        View::assign($view);
        return view();
    }
	public function edit(){
		$id = input('id');
	    if (Request::isPost()) {
	    	$params = $this->request->post();
	        $data = [
				'name'=>$params['name'],
				'bus_code'=>$params['bus_code'],
				'url_address'=>$params['url_address'],
				'update_time'=>time(),
			];
	        $res = Db::name('page')->where('id',$id)->update($data);
	        if ($res) {
	            $this->success(lang('edit success'), url('index'));
	        } else {
	            $this->error(lang('edit fail'));
	        }
	    }
	    $info = Db::name('page')->where('id',$id)->find();
	    $view = [
	        'info' => $info,
	        'title' => '修改',
	    ];
	    View::assign($view);
	    return view('add');
	}
    public function detail(){
    	$id = input('id');
    	$addtime = input('addtime');
		$condition[] = ['url_id', '=', $id];
		if (!empty($addtime) && isset($addtime)) {
			$time = explode(' - ',$addtime);
		    $condition[] = ['add_time', 'between', [strtotime($time[0]),strtotime($time[1])+86399]];
		}
        //首先查找事件
        $list = Db::name('event')->where('status',1)->select()->toArray();
        $info = Db::name('monitor')->where($condition)->select();
		$res = [];
		//整理数据
        foreach ($list as $key => $value) {
			$res[] = $value['name'];
			$result[$key]['value'] = 0;
			$result[$key]['name'] = $value['name'];
            foreach ($info as $k => $v) {
				if($v['event_id'] == $value['id']){
					$result[$key]['value']++;
				}
            }
        }
		foreach($result as $key => $value){
			$result[$key]['name'] = $value['name'].'('.$value['value'].')次';
		}
        $view = [
            'res' => $res,
            'result' => $result,
            'id' => $id,
            'addtime' => $addtime,
        ];
        View::assign($view);
        return view();
    }
}